package com.jungle.sp.daily;

import cn.hutool.core.date.DateUtil;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.IOException;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.List;

public class JsonMappingExample {

    public static void main(String[] args) {
        String jsonString = "{\n" +
                "    \"MsgType\": 2,\n" +
                "    \"Data\": [{\n" +
                "        \"SessionNo\": \"acbc336b-2eec-4991-a0d5-36eeaa2a13c8\",\n" +
                "        \"Phone\": \"918454815335\",\n" +
                "        \"UserId\": \"1041\",\n" +
                "        \"CostCount\": 1,\n" +
                "        \"ReceiptTime\": 1731293645,\n" +
                "        \"ReceiptResult\": \"发送成功\",\n" +
                "        \"ReceiptCode\": \"0\",\n" +
                "        \"ReceiptDesc\": \"发送成功\"\n" +
                "    }]\n" +
                "}";

        ObjectMapper objectMapper = new ObjectMapper();

        try {
            SmsReceiveStatus smsReceiveStatus = objectMapper.readValue(jsonString, SmsReceiveStatus.class);
            System.out.println(DateUtil.format(smsReceiveStatus.getData().get(0).getReceiptTime(), "yyyy-MM-dd HH:mm:ss"));
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
    }

    @NoArgsConstructor
    @Data
    public static class SmsReceiveStatus {

        @JsonProperty("MsgType")
        private Integer msgType;

        @JsonProperty("Data")
        private List<DataDTO> data;

        @NoArgsConstructor
        @Data
        public static class DataDTO {

            @JsonProperty("SessionNo")
            private String sessionNo;

            @JsonProperty("Phone")
            private String phone;

            @JsonProperty("UserId")
            private String userId;

            @JsonProperty("CostCount")
            private Integer costCount;

            @JsonProperty("ReceiptResult")
            private String receiptResult;

            @JsonProperty("ReceiptCode")
            private String receiptCode;

            @JsonProperty("ReceiptDesc")
            private String receiptDesc;

            @JsonProperty("ReceiptTime")
            // @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "Asia/Shanghai")
            @JsonDeserialize(using = UnixTimestampDeserializer.class)
            private LocalDateTime receiptTime;
        }
    }

    // 自定义反序列化器，将 Unix 时间戳转换为 LocalDateTime
    public static class UnixTimestampDeserializer extends JsonDeserializer<LocalDateTime> {
        @Override
        public LocalDateTime deserialize(JsonParser p, DeserializationContext ctxt) throws IOException, JsonProcessingException {
            long timestamp = p.getLongValue();
            return LocalDateTime.ofInstant(Instant.ofEpochSecond(timestamp), ZoneId.of("Asia/Shanghai"));
        }
    }
}
